import requests

# 接口配置（使用您提供的真实参数）
AUTH_URL = "https://xuntian-cloud-test3.tclpv.com/xts_api/auth/oauth2/token"
AUTH_PARAMS = {
    "username": "kt_wuzex",
    "randomStr": "blockPuzzle",
    "code": "J+8qgxLklrB9CQt/WgMx7Vumk5VTKxbDHhn77bkFXF4NGrWANvq0sN8UwhE8z2zl9zS5S62XOghRwYd9Ixz5HQRkuYcpCPH14MucSlupn70=",  # 注意：实际使用时需动态获取
    "grant_type": "password",
    "scope": "server"
}
AUTH_DATA = {"password": "YQfy1JBZ"}  # 密码明文传输（生产环境需加密）

def call_auth_api():
    """直接调用认证接口"""
    try:
        # 1. 发送认证请求
        response = requests.post(
            AUTH_URL,
            params=AUTH_PARAMS,
            data=AUTH_DATA,
            headers={
                "Content-Type": "application/x-www-form-urlencoded",
                "User-Agent": "Mozilla/5.0"
            },
            timeout=10  # 设置超时
        )

        # 2. 处理响应
        if response.status_code == 200:
            token_data = response.json()
            print("✅ 认证成功！")
            print(f"Access Token: {token_data.get('access_token')[:15]}...")
            print(f"Refresh Token: {token_data.get('refresh_token')[:15]}...")
            return token_data
        else:
            print(f"❌ 认证失败 (HTTP {response.status_code}): {response.text}{response.content}")
            return None

    except Exception as e:
        print(f"⚠️ 请求异常: {str(e)}")
        return None

# 执行调用
if __name__ == "__main__":
    auth_result = call_auth_api()
    
    # 示例：使用获取的token调用其他API
    if auth_result:
        print("\n尝试获取用户信息...")
        user_info = requests.get(
            "https://xuntian-cloud-test3.tclpv.com/api/user/info",
            headers={"Authorization": f"Bearer {auth_result['access_token']}"}
        ).json()
        print("用户信息:", user_info)